// ListToTree.ts

interface TreeNode {
  value: string | number;
  label: string;
  children?: TreeNode[];
}

interface ListToTreeOptions {
  idKey?: string; // 列表中表示节点ID的字段名，默认 'id'
  parentIdKey?: string; // 列表中表示父节点ID的字段名，默认 'parentId'
  labelKey?: string; // 列表中表示显示文本的字段名，默认 'label'
}

export class ListToTree {
  private readonly idKey: string;
  private readonly parentIdKey: string;
  private readonly labelKey: string;

  constructor(options: ListToTreeOptions = {}) {
    this.idKey = options.idKey || "id";
    this.parentIdKey = options.parentIdKey || "parentId";
    this.labelKey = options.labelKey || "label";
  }

  /**
   * 将扁平列表转换为树形结构
   * @param list 扁平数据列表
   * @returns 树形结构数组
   */
  transform<T extends Record<string, any>>(list: T[]): TreeNode[] {
    if (!Array.isArray(list)) {
      throw new Error("Input must be an array");
    }

    const tree: TreeNode[] = [];
    const map = new Map<any, TreeNode>();

    // 第一步：先将所有节点初始化为树节点，并用 map 存储引用
    list.forEach((item) => {
      const id = item[this.idKey];
      // const parentId = item[this.parentIdKey];
      const label = item[this.labelKey] ?? String(id);

      map.set(id, {
        value: id,
        label: String(label),
        children: []
      });
    });

    // 第二步：建立父子关系
    list.forEach((item) => {
      const id = item[this.idKey];
      const parentId = item[this.parentIdKey];
      const node = map.get(id);

      if (node) {
        if (parentId === null || parentId === undefined || parentId === "") {
          // 根节点
          tree.push(node);
        } else {
          const parent = map.get(parentId);
          if (parent) {
            parent.children = parent.children || [];
            parent.children.push(node);
          }
        }
      }
    });

    // 过滤掉空的 children 数组
    const cleanChildren = (nodes: TreeNode[]): TreeNode[] => {
      return nodes.map((node) => {
        if (node.children && node.children.length > 0) {
          node.children = cleanChildren(node.children);
        } else {
          delete node.children; // 可选：若不需要空 children，可删除
        }
        return node;
      });
    };

    return cleanChildren(tree);
  }
}
